package com.actionsky.dao;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sphx.api.SphinxClient;
import org.sphx.api.SphinxException;

public class ActionskyUtil {
	private static Log _log = LogFactory.getLog(ActionskyUtil.class);
	
	public static Connection getConnection() {
        Connection conn = null;
        try {
            Context initCtx = new InitialContext();
            Context ctx = (Context) initCtx.lookup("java:comp/env");
            Object obj = (Object) ctx.lookup("jdbc/mysql_actionsky");
            DataSource ds = (javax.sql.DataSource) obj;
            conn = ds.getConnection();
        } catch (Exception ex) {
        	_log.error("数据库连接异常!",ex);
            ex.printStackTrace();
        }
        return conn;
    }
	
	/**
	 * get connect from full text index server
	 **/
	public static Connection getConnectionFromSearchServer(){
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://www.etuan.com:9306","","");
        } catch (Exception ex) {
        	_log.error("数据库连接异常!",ex);
            ex.printStackTrace();
        }
        return conn;
	}
	
	/**
	 * get connect from full text index server
	 **/
	public static SphinxClient getSphinxClient(){
		String host = "www.etuan.com";
		int port = 9312;
		int mode = SphinxClient.SPH_MATCH_ALL;
		SphinxClient cl = new SphinxClient();
		try {
			cl.setServer ( host, port );
			cl.setWeights ( new int[] { 100, 1 } );
			cl.setMatchMode ( mode );
		} catch (SphinxException e) {
			_log.error("SphinxClient初始化失败",e);
			e.printStackTrace();
		}
		return cl;
	}
}
